//
//  BSEssenceViewController.m
//  Baisijie
//
//  Created by 王建伟 on 2017/3/1.
//  Copyright © 2017年 RangGuangyu. All rights reserved.
//

#import "BSEssenceViewController.h"
#import "UIBarButtonItem+Extension.h"
#import <AFNetworking.h>
#import "BSTopMenuItem.h"
#import <MJExtension.h>
#import "BSTopMenuView.h"
#import "BSContentViewController.h"


@interface BSEssenceViewController ()<UIScrollViewDelegate>

/** 滚动视图 */
@property (nonatomic ,weak) UIScrollView *scrollView;
/** 标题数组 */
@property (nonatomic ,strong) NSMutableArray *submenus;
/** 顶部菜单视图 */
@property (nonatomic ,weak) BSTopMenuView *topMenuView;
/** 是否为边缘 */
@property (nonatomic ,assign) BOOL isEdge;

@end

static NSString *const ID = @"BSTopicCollectionViewCell";

@implementation BSEssenceViewController

- (BSTopMenuView *)topMenuView {
    if (!_topMenuView) {
        BSTopMenuView *topMenuView = [[BSTopMenuView alloc] initWithFrame:CGRectMake(0, -BSStatusBarHeight, KScreenW, BSTopicViewHeight)];
        [topMenuView setTopicBtnClickBlock:^(NSInteger index) {
            [self addChildVcViewToScrollView:index];
        }];
        [self.view addSubview:topMenuView];
        _topMenuView = topMenuView;
    }
    return _topMenuView;
}
- (void)setupNav {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
    self.navigationItem.titleView = imageView;
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"nav_item_game_icon" highlightImage:@"nav_item_game_click_icon" target:self action:@selector(leftItemClick)];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"RandomAcross" highlightImage:@"RandomAcrossClick" target:self action:@selector(rightItemClick)];
}
- (void)leftItemClick {
    BSLog(@"%s",__func__);
}
- (void)rightItemClick {
    BSLog(@"%s",__func__);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNav];
    [self setupScrollView];
    [self getTopTitles];
    [BSNotificationCenter addObserver:self selector:@selector(hideNavigationBar) name:BSScrollViewPanUpNotification object:nil];
    [BSNotificationCenter addObserver:self selector:@selector(showNavigationBar) name:BSScrollViewPanDownNotification object:nil];
}
#pragma mark - 隐藏顶部导航栏
- (void)hideNavigationBar {
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [UIView animateWithDuration:0.1 animations:^{
        self.topMenuView.bs_y = 0;
        self.scrollView.bs_height = KScreenH - BSStatusBarHeight;
    }];
}
#pragma mark - 显示顶部导航栏
- (void)showNavigationBar {
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [UIView animateWithDuration:0.1 animations:^{
        self.topMenuView.bs_y = - BSStatusBarHeight;
        self.scrollView.bs_height = KScreenH - 3 * BSStatusBarHeight;
    }];
}
- (void)dealloc {
    [BSNotificationCenter removeObserver:self];
}
- (void)setupAllChildVc {
    for (int i = 0; i < _submenus.count; i ++) {
        BSContentViewController *vc = [BSContentViewController new];
        BSTopMenuItem *item = self.submenus[i];
        vc.title = item.name;
        vc.url = item.url;
        [self addChildViewController:vc];
    }
    _scrollView.contentSize = CGSizeMake(KScreenW * _submenus.count, 0);
}

/**
 添加自控制器的view到scrollView中

 @param index 子控制器的索引
 */
- (void)addChildVcViewToScrollView:(NSInteger)index {
    CGFloat width = _scrollView.bs_width;
    UIView *view = self.childViewControllers[index].view;
    view.frame = CGRectMake(width * index, 0, width, _scrollView.bs_height);
    [_scrollView addSubview:view];
    [_scrollView setContentOffset:CGPointMake(width * index, _scrollView.contentOffset.y) animated:YES];
}
- (void)setupScrollView {
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    scrollView.bs_height -= BSNavBarHeight;
    scrollView.backgroundColor = BSRGBColor(240, 240, 240, 1);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    _scrollView = scrollView;
}
#pragma mark - 获取顶部标题
- (void)getTopTitles {
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    [mgr GET:@"http://s.budejie.com/public/list-appbar/bs0315-iphone-4.5.4" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
//        BSLog(@"%@",responseObject)
        NSArray *menus = responseObject[@"menus"];
        NSDictionary *essenceMenus = menus.firstObject;
        NSArray *submenus = essenceMenus[@"submenus"];
        self.submenus = [BSTopMenuItem mj_objectArrayWithKeyValuesArray:submenus];
        [self setupAllChildVc];
        self.topMenuView.dataSource = self.submenus;
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error.code != NSURLErrorCancelled) {
            [SVProgressHUD showErrorWithStatus:@"加载失败"];
        }
    }];
}
#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    CGPoint offset = scrollView.contentOffset;
    _isEdge = (offset.x < 0 || offset.x > scrollView.contentSize.width - scrollView.bs_width);
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGPoint offset = scrollView.contentOffset;
    if (_isEdge) return;
    NSInteger index = offset.x / KScreenW;
    if (index == self.topMenuView.selectedIndex) return;
    self.topMenuView.selectedIndex = index;
}


@end
